home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / ds3100.md / gdb / mips-tdep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-01  |  25.1 KB  |  821 lines

  1. /* Target-dependent code for the MIPS architecture, for GDB, the GNU Debugger.
  2.    Copyright 1988, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  3.    Contributed by Alessandro Forin(af@cs.cmu.edu) at CMU
  4.    and by Per Bothner(bothner@cs.wisc.edu) at U.Wisconsin.
  5.  
  6. This file is part of GDB.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include "defs.h"
  23. #include "frame.h"
  24. #include "inferior.h"
  25. #include "symtab.h"
  26. #include "value.h"
  27. #include "gdbcmd.h"
  28. #include "language.h"
  29.  
  30. #ifdef USG
  31. #include <sys/types.h>
  32. #endif
  33.  
  34. #include <sys/param.h>
  35. #include <sys/dir.h>
  36. #include <signal.h>
  37. #include <sys/ioctl.h>
  38.  
  39. #include <setjmp.h>
  40.  
  41. #include "gdbcore.h"
  42. #include "symfile.h"
  43. #include "objfiles.h"
  44.  
  45. #ifndef    MIPSMAGIC
  46. #ifdef MIPSEL
  47. #define MIPSMAGIC    MIPSELMAGIC
  48. #else
  49. #define MIPSMAGIC    MIPSEBMAGIC
  50. #endif
  51. #endif
  52.  
  53. #define VM_MIN_ADDRESS (unsigned)0x400000
  54.  
  55. #include <sys/user.h>        /* After a.out.h  */
  56. #include <sys/file.h>
  57. #include <sys/stat.h>
  58.  
  59.  
  60. #define PROC_LOW_ADDR(proc) ((proc)->pdr.adr) /* least address */
  61. #define PROC_HIGH_ADDR(proc) ((proc)->pdr.iline) /* upper address bound */
  62. #define PROC_FRAME_OFFSET(proc) ((proc)->pdr.frameoffset)
  63. #define PROC_FRAME_REG(proc) ((proc)->pdr.framereg)
  64. #define PROC_REG_MASK(proc) ((proc)->pdr.regmask)
  65. #define PROC_FREG_MASK(proc) ((proc)->pdr.fregmask)
  66. #define PROC_REG_OFFSET(proc) ((proc)->pdr.regoffset)
  67. #define PROC_FREG_OFFSET(proc) ((proc)->pdr.fregoffset)
  68. #define PROC_PC_REG(proc) ((proc)->pdr.pcreg)
  69. #define PROC_SYMBOL(proc) (*(struct symbol**)&(proc)->pdr.isym)
  70. #define _PROC_MAGIC_ 0x0F0F0F0F
  71. #define PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym == _PROC_MAGIC_)
  72. #define SET_PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym = _PROC_MAGIC_)
  73.  
  74. struct linked_proc_info
  75. {
  76.   struct mips_extra_func_info info;
  77.   struct linked_proc_info *next;
  78. } * linked_proc_desc_table = NULL;
  79.  
  80.  
  81. #define READ_FRAME_REG(fi, regno) read_next_frame_reg((fi)->next, regno)
  82.  
  83. static int
  84. read_next_frame_reg(fi, regno)
  85.      FRAME fi;
  86.      int regno;
  87. {
  88. #define SIGFRAME_BASE   sizeof(struct sigcontext)
  89. #define SIGFRAME_PC_OFF (-SIGFRAME_BASE+ 2*sizeof(int))
  90. #define SIGFRAME_SP_OFF (-SIGFRAME_BASE+32*sizeof(int))
  91. #define SIGFRAME_RA_OFF (-SIGFRAME_BASE+34*sizeof(int))
  92.   for (; fi; fi = fi->next)
  93.       if (in_sigtramp(fi->pc, 0)) {
  94.       /* No idea if this code works. --PB. */
  95.       int offset;
  96.       if (regno == PC_REGNUM) offset = SIGFRAME_PC_OFF;
  97.       else if (regno == RA_REGNUM) offset = SIGFRAME_RA_OFF;
  98.       else if (regno == SP_REGNUM) offset = SIGFRAME_SP_OFF;
  99.       else return 0;
  100.       return read_memory_integer(fi->frame + offset, 4);
  101.       }
  102.       else if (regno == SP_REGNUM) return fi->frame;
  103.       else if (fi->saved_regs->regs[regno])
  104.     return read_memory_integer(fi->saved_regs->regs[regno], 4);
  105.   return read_register(regno);
  106. }
  107.  
  108. int
  109. mips_frame_saved_pc(frame)
  110.      FRAME frame;
  111. {
  112.   mips_extra_func_info_t proc_desc = frame->proc_desc;
  113.   int pcreg = proc_desc ? PROC_PC_REG(proc_desc) : RA_REGNUM;
  114.  
  115.   if (proc_desc && PROC_DESC_IS_DUMMY(proc_desc))
  116.       return read_memory_integer(frame->frame - 4, 4);
  117.  
  118.   return read_next_frame_reg(frame, pcreg);
  119. }
  120.  
  121. static struct mips_extra_func_info temp_proc_desc;
  122. static struct frame_saved_regs temp_saved_regs;
  123.  
  124. static CORE_ADDR
  125. heuristic_proc_start(pc)
  126.     CORE_ADDR pc;
  127. {
  128.  
  129.     CORE_ADDR start_pc = pc;
  130.     CORE_ADDR fence = start_pc - 200;
  131.     if (fence < VM_MIN_ADDRESS) fence = VM_MIN_ADDRESS;
  132.     /* search back for previous return */
  133.     for (start_pc -= 4; ; start_pc -= 4)
  134.     if (start_pc < fence) return 0; 
  135.     else if (ABOUT_TO_RETURN(start_pc))
  136.         break;
  137.  
  138.     start_pc += 8; /* skip return, and its delay slot */
  139. #if 0
  140.     /* skip nops (usually 1) 0 - is this */
  141.     while (start_pc < pc && read_memory_integer (start_pc, 4) == 0)
  142.     start_pc += 4;
  143. #endif
  144.     return start_pc;
  145. }
  146.  
  147. static mips_extra_func_info_t
  148. heuristic_proc_desc(start_pc, limit_pc, next_frame)
  149.     CORE_ADDR start_pc, limit_pc;
  150.     FRAME next_frame;
  151. {
  152.     CORE_ADDR sp = next_frame ? next_frame->frame : read_register (SP_REGNUM);
  153.     CORE_ADDR cur_pc;
  154.     int frame_size;
  155.     int has_frame_reg = 0;
  156.     int reg30; /* Value of $r30. Used by gcc for frame-pointer */
  157.     unsigned long reg_mask = 0;
  158.  
  159.     if (start_pc == 0) return NULL;
  160.     bzero(&temp_proc_desc, sizeof(temp_proc_desc));
  161.     bzero(&temp_saved_regs, sizeof(struct frame_saved_regs));
  162.     if (start_pc + 200 < limit_pc) limit_pc = start_pc + 200;
  163.   restart:
  164.     frame_size = 0;
  165.     for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += 4) {
  166.     unsigned long word;
  167.     int status;
  168.  
  169.     status = read_memory_nobpt (cur_pc, &word, 4); 
  170.     if (status) memory_error (status, cur_pc); 
  171.     SWAP_TARGET_AND_HOST (&word, sizeof (word));
  172.     if ((word & 0xFFFF0000) == 0x27bd0000) /* addiu $sp,$sp,-i */
  173.         frame_size += (-word) & 0xFFFF;
  174.     else if ((word & 0xFFFF0000) == 0x23bd0000) /* addu $sp,$sp,-i */
  175.         frame_size += (-word) & 0xFFFF;
  176.     else if ((word & 0xFFE00000) == 0xafa00000) { /* sw reg,offset($sp) */
  177.         int reg = (word & 0x001F0000) >> 16;
  178.         reg_mask |= 1 << reg;
  179.         temp_saved_regs.regs[reg] = sp + (short)word;
  180.     }
  181.     else if ((word & 0xFFFF0000) == 0x27be0000) { /* addiu $30,$sp,size */
  182.         if ((unsigned short)word != frame_size)
  183.         reg30 = sp + (unsigned short)word;
  184.         else if (!has_frame_reg) {
  185.         int alloca_adjust;
  186.         has_frame_reg = 1;
  187.         reg30 = read_next_frame_reg(next_frame, 30);
  188.         alloca_adjust = reg30 - (sp + (unsigned short)word);
  189.         if (alloca_adjust > 0) {
  190.             /* FP > SP + frame_size. This may be because
  191.             /* of an alloca or somethings similar.
  192.              * Fix sp to "pre-alloca" value, and try again.
  193.              */
  194.             sp += alloca_adjust;
  195.             goto restart;
  196.         }
  197.         }
  198.     }
  199.     else if ((word & 0xFFE00000) == 0xafc00000) { /* sw reg,offset($30) */
  200.         int reg = (word & 0x001F0000) >> 16;
  201.         reg_mask |= 1 << reg;
  202.         temp_saved_regs.regs[reg] = reg30 + (short)word;
  203.     }
  204.     }
  205.     if (has_frame_reg) {
  206.     PROC_FRAME_REG(&temp_proc_desc) = 30;
  207.     PROC_FRAME_OFFSET(&temp_proc_desc) = 0;
  208.     }
  209.     else {
  210.     PROC_FRAME_REG(&temp_proc_desc) = SP_REGNUM;
  211.     PROC_FRAME_OFFSET(&temp_proc_desc) = frame_size;
  212.     }
  213.     PROC_REG_MASK(&temp_proc_desc) = reg_mask;
  214.     PROC_PC_REG(&temp_proc_desc) = RA_REGNUM;
  215.     return &temp_proc_desc;
  216. }
  217.  
  218. static mips_extra_func_info_t
  219. find_proc_desc(pc, next_frame)
  220.     CORE_ADDR pc;
  221.     FRAME next_frame;
  222. {
  223.   mips_extra_func_info_t proc_desc;
  224.   struct block *b = block_for_pc(pc);
  225.   struct symbol *sym =
  226.       b ? lookup_symbol(".gdbinfo.", b, LABEL_NAMESPACE, 0, NULL) : NULL;
  227.  
  228.   if (sym)
  229.     {
  230.     /* IF this is the topmost frame AND
  231.      * (this proc does not have debugging information OR
  232.      * the PC is in the procedure prologue)
  233.      * THEN create a "heuristic" proc_desc (by analyzing
  234.      * the actual code) to replace the "official" proc_desc.
  235.      */
  236.     proc_desc = (mips_extra_func_info_t)SYMBOL_VALUE(sym);
  237.     if (next_frame == NULL) {
  238.         struct symtab_and_line val;
  239.         struct symbol *proc_symbol =
  240.         PROC_DESC_IS_DUMMY(proc_desc) ? 0 : PROC_SYMBOL(proc_desc);
  241.  
  242.         if (proc_symbol) {
  243.         val = find_pc_line (BLOCK_START
  244.                     (SYMBOL_BLOCK_VALUE(proc_symbol)),
  245.                     0);
  246.         val.pc = val.end ? val.end : pc;
  247.         }
  248.         if (!proc_symbol || pc < val.pc) {
  249.         mips_extra_func_info_t found_heuristic =
  250.             heuristic_proc_desc(PROC_LOW_ADDR(proc_desc),
  251.                     pc, next_frame);
  252.         if (found_heuristic) proc_desc = found_heuristic;
  253.         }
  254.     }
  255.     }
  256.   else
  257.     {
  258.       /* Is linked_proc_desc_table really necessary?  It only seems to be used
  259.      by procedure call dummys.  However, the procedures being called ought
  260.      to have their own proc_descs, and even if they don't,
  261.      heuristic_proc_desc knows how to create them! */
  262.  
  263.       register struct linked_proc_info *link;
  264.       for (link = linked_proc_desc_table; link; link = link->next)
  265.       if (PROC_LOW_ADDR(&link->info) <= pc
  266.           && PROC_HIGH_ADDR(&link->info) > pc)
  267.           return &link->info;
  268.       proc_desc =
  269.       heuristic_proc_desc(heuristic_proc_start(pc), pc, next_frame);
  270.     }
  271.   return proc_desc;
  272. }
  273.  
  274. mips_extra_func_info_t cached_proc_desc;
  275.  
  276. FRAME_ADDR
  277. mips_frame_chain(frame)
  278.     FRAME frame;
  279. {
  280.     mips_extra_func_info_t proc_desc;
  281.     CORE_ADDR saved_pc = FRAME_SAVED_PC(frame);
  282.  
  283.     if (saved_pc == 0 || inside_entry_file (saved_pc))
  284.       return 0;
  285.  
  286.     proc_desc = find_proc_desc(saved_pc, frame);
  287.     if (!proc_desc)
  288.       return 0;
  289.  
  290.     cached_proc_desc = proc_desc;
  291.     return read_next_frame_reg(frame, PROC_FRAME_REG(proc_desc))
  292.       + PROC_FRAME_OFFSET(proc_desc);
  293. }
  294.  
  295. void
  296. init_extra_frame_info(fci)
  297.      struct frame_info *fci;
  298. {
  299.   extern struct obstack frame_cache_obstack;
  300.   /* Use proc_desc calculated in frame_chain */
  301.   mips_extra_func_info_t proc_desc = fci->next ? cached_proc_desc :
  302.       find_proc_desc(fci->pc, fci->next);
  303.  
  304.   fci->saved_regs = (struct frame_saved_regs*)
  305.     obstack_alloc (&frame_cache_obstack, sizeof(struct frame_saved_regs));
  306.   bzero(fci->saved_regs, sizeof(struct frame_saved_regs));
  307.   fci->proc_desc =
  308.       proc_desc == &temp_proc_desc ? 0 : proc_desc;
  309.   if (proc_desc)
  310.     {
  311.       int ireg;
  312.       CORE_ADDR reg_position;
  313.       unsigned long mask;
  314.       /* r0 bit means kernel trap */
  315.       int kernel_trap = PROC_REG_MASK(proc_desc) & 1;
  316.  
  317.       /* Fixup frame-pointer - only needed for top frame */
  318.       /* This may not be quite right, if procedure has a real frame register */
  319.       if (fci->pc == PROC_LOW_ADDR(proc_desc))
  320.       fci->frame = read_register (SP_REGNUM);
  321.       else
  322.       fci->frame = READ_FRAME_REG(fci, PROC_FRAME_REG(proc_desc))
  323.           + PROC_FRAME_OFFSET(proc_desc);
  324.  
  325.       if (proc_desc == &temp_proc_desc)
  326.       *fci->saved_regs = temp_saved_regs;
  327.       else
  328.       {
  329.       /* find which general-purpose registers were saved */
  330.       reg_position = fci->frame + PROC_REG_OFFSET(proc_desc);
  331.       mask = kernel_trap ? 0xFFFFFFFF : PROC_REG_MASK(proc_desc);
  332.       for (ireg= 31; mask; --ireg, mask <<= 1)
  333.           if (mask & 0x80000000)
  334.           {
  335.           fci->saved_regs->regs[ireg] = reg_position;
  336.           reg_position -= 4;
  337.           }
  338.       /* find which floating-point registers were saved */
  339.       reg_position = fci->frame + PROC_FREG_OFFSET(proc_desc);
  340.       /* The freg_offset points to where the first *double* register is saved.
  341.        * So skip to the high-order word. */
  342.       reg_position += 4;
  343.       mask = kernel_trap ? 0xFFFFFFFF : PROC_FREG_MASK(proc_desc);
  344.       for (ireg = 31; mask; --ireg, mask <<= 1)
  345.           if (mask & 0x80000000)
  346.           {
  347.           fci->saved_regs->regs[FP0_REGNUM+ireg] = reg_position;
  348.           reg_position -= 4;
  349.           }
  350.       }
  351.  
  352.       /* hack: if argument regs are saved, guess these contain args */
  353.       if ((PROC_REG_MASK(proc_desc) & 0xF0) == 0) fci->num_args = -1;
  354.       else if ((PROC_REG_MASK(proc_desc) & 0x80) == 0) fci->num_args = 4;
  355.       else if ((PROC_REG_MASK(proc_desc) & 0x40) == 0) fci->num_args = 3;
  356.       else if ((PROC_REG_MASK(proc_desc) & 0x20) == 0) fci->num_args = 2;
  357.       else if ((PROC_REG_MASK(proc_desc) & 0x10) == 0) fci->num_args = 1;
  358.  
  359.       fci->saved_regs->regs[PC_REGNUM] = fci->saved_regs->regs[RA_REGNUM];
  360.     }
  361. }
  362.  
  363.  
  364. CORE_ADDR
  365. mips_push_arguments(nargs, args, sp, struct_return, struct_addr)
  366.   int nargs;
  367.   value *args;
  368.   CORE_ADDR sp;
  369.   int struct_return;
  370.   CORE_ADDR struct_addr;
  371. {
  372.   CORE_ADDR buf;
  373.   register i;
  374.   int accumulate_size = struct_return ? 4 : 0;
  375.   struct mips_arg { char *contents; int len; int offset; };
  376.   struct mips_arg *mips_args =
  377.       (struct mips_arg*)alloca(nargs * sizeof(struct mips_arg));
  378.   register struct mips_arg *m_arg;
  379.   for (i = 0, m_arg = mips_args; i < nargs; i++, m_arg++) {
  380.     extern value value_arg_coerce();
  381.     value arg = value_arg_coerce (args[i]);
  382.     m_arg->len = TYPE_LENGTH (VALUE_TYPE (arg));
  383.     /* This entire mips-specific routine is because doubles must be aligned
  384.      * on 8-byte boundaries. It still isn't quite right, because MIPS decided
  385.      * to align 'struct {int a, b}' on 4-byte boundaries (even though this
  386.      * breaks their varargs implementation...). A correct solution
  387.      * requires an simulation of gcc's 'alignof' (and use of 'alignof'
  388.      * in stdarg.h/varargs.h).
  389.      */
  390.     if (m_arg->len > 4) accumulate_size = (accumulate_size + 7) & -8;
  391.     m_arg->offset = accumulate_size;
  392.     accumulate_size = (accumulate_size + m_arg->len + 3) & -4;
  393.     m_arg->contents = VALUE_CONTENTS(arg);
  394.   }
  395.   accumulate_size = (accumulate_size + 7) & (-8);
  396.   if (accumulate_size < 16) accumulate_size = 16; 
  397.   sp -= accumulate_size;
  398.   for (i = nargs; m_arg--, --i >= 0; )
  399.     write_memory(sp + m_arg->offset, m_arg->contents, m_arg->len);
  400.   if (struct_return) {
  401.     buf = struct_addr;
  402.     write_memory(sp, &buf, sizeof(CORE_ADDR));
  403. }
  404.   return sp;
  405. }
  406.  
  407. /* MASK(i,j) == (1<<i) + (1<<(i+1)) + ... + (1<<j)). Assume i<=j<31. */
  408. #define MASK(i,j) ((1 << (j)+1)-1 ^ (1 << (i))-1)
  409.  
  410. void
  411. mips_push_dummy_frame()
  412. {
  413.   int ireg;
  414.   struct linked_proc_info *link = (struct linked_proc_info*)
  415.       xmalloc(sizeof(struct linked_proc_info));
  416.   mips_extra_func_info_t proc_desc = &link->info;
  417.   CORE_ADDR sp = read_register (SP_REGNUM);
  418.   CORE_ADDR save_address;
  419.   REGISTER_TYPE buffer;
  420.   link->next = linked_proc_desc_table;
  421.   linked_proc_desc_table = link;
  422. #define PUSH_FP_REGNUM 16 /* must be a register preserved across calls */
  423. #define GEN_REG_SAVE_MASK MASK(1,16)|MASK(24,28)|(1<<31)
  424. #define GEN_REG_SAVE_COUNT 22
  425. #define FLOAT_REG_SAVE_MASK MASK(0,19)
  426. #define FLOAT_REG_SAVE_COUNT 20
  427. #define SPECIAL_REG_SAVE_COUNT 4
  428.   /*
  429.    * The registers we must save are all those not preserved across
  430.    * procedure calls. Dest_Reg (see tm-mips.h) must also be saved.
  431.    * In addition, we must save the PC, and PUSH_FP_REGNUM.
  432.    * (Ideally, we should also save MDLO/-HI and FP Control/Status reg.)
  433.    *
  434.    * Dummy frame layout:
  435.    *  (high memory)
  436.    *     Saved PC
  437.    *    Saved MMHI, MMLO, FPC_CSR
  438.    *    Saved R31
  439.    *    Saved R28
  440.    *    ...
  441.    *    Saved R1
  442.    *    Saved D18 (i.e. F19, F18)
  443.    *    ...
  444.    *    Saved D0 (i.e. F1, F0)
  445.    *    CALL_DUMMY (subroutine stub; see m-mips.h)
  446.    *    Parameter build area (not yet implemented)
  447.    *  (low memory)
  448.    */
  449.   PROC_REG_MASK(proc_desc) = GEN_REG_SAVE_MASK;
  450.   PROC_FREG_MASK(proc_desc) = FLOAT_REG_SAVE_MASK;
  451.   PROC_REG_OFFSET(proc_desc) = /* offset of (Saved R31) from FP */
  452.       -sizeof(long) - 4 * SPECIAL_REG_SAVE_COUNT;
  453.   PROC_FREG_OFFSET(proc_desc) = /* offset of (Saved D18) from FP */
  454.       -sizeof(double) - 4 * (SPECIAL_REG_SAVE_COUNT + GEN_REG_SAVE_COUNT);
  455.   /* save general registers */
  456.   save_address = sp + PROC_REG_OFFSET(proc_desc);
  457.   for (ireg = 32; --ireg >= 0; )
  458.     if (PROC_REG_MASK(proc_desc) & (1 << ireg))
  459.       {
  460.     buffer = read_register (ireg);
  461.     write_memory (save_address, &buffer, sizeof(REGISTER_TYPE));
  462.     save_address -= 4;
  463.       }
  464.   /* save floating-points registers */
  465.   save_address = sp + PROC_FREG_OFFSET(proc_desc);
  466.   for (ireg = 32; --ireg >= 0; )
  467.     if (PROC_FREG_MASK(proc_desc) & (1 << ireg))
  468.       {
  469.     buffer = read_register (ireg + FP0_REGNUM);
  470.     write_memory (save_address, &buffer, 4);
  471.     save_address -= 4;
  472.       }
  473.   write_register (PUSH_FP_REGNUM, sp);
  474.   PROC_FRAME_REG(proc_desc) = PUSH_FP_REGNUM;
  475.   PROC_FRAME_OFFSET(proc_desc) = 0;
  476.   buffer = read_register (PC_REGNUM);
  477.   write_memory (sp - 4, &buffer, sizeof(REGISTER_TYPE));
  478.   buffer = read_register (HI_REGNUM);
  479.   write_memory (sp - 8, &buffer, sizeof(REGISTER_TYPE));
  480.   buffer = read_register (LO_REGNUM);
  481.   write_memory (sp - 12, &buffer, sizeof(REGISTER_TYPE));
  482.   buffer = read_register (FCRCS_REGNUM);
  483.   write_memory (sp - 16, &buffer, sizeof(REGISTER_TYPE));
  484.   sp -= 4 * (GEN_REG_SAVE_COUNT+FLOAT_REG_SAVE_COUNT+SPECIAL_REG_SAVE_COUNT);
  485.   write_register (SP_REGNUM, sp);
  486.   PROC_LOW_ADDR(proc_desc) = sp - CALL_DUMMY_SIZE + CALL_DUMMY_START_OFFSET;
  487.   PROC_HIGH_ADDR(proc_desc) = sp;
  488.   SET_PROC_DESC_IS_DUMMY(proc_desc);
  489.   PROC_PC_REG(proc_desc) = RA_REGNUM;
  490. }
  491.  
  492. void
  493. mips_pop_frame()
  494. { register int regnum;
  495.   FRAME frame = get_current_frame ();
  496.   CORE_ADDR new_sp = frame->frame;
  497.   mips_extra_func_info_t proc_desc = (mips_extra_func_info_t)frame->proc_desc;
  498.   if (PROC_DESC_IS_DUMMY(proc_desc))
  499.     {
  500.       struct linked_proc_info **ptr = &linked_proc_desc_table;;
  501.       for (; &ptr[0]->info != proc_desc; ptr = &ptr[0]->next )
  502.       if (ptr[0] == NULL) abort();
  503.       *ptr = ptr[0]->next;
  504.       free (ptr[0]);
  505.       write_register (HI_REGNUM, read_memory_integer(new_sp - 8, 4));
  506.       write_register (LO_REGNUM, read_memory_integer(new_sp - 12, 4));
  507.       write_register (FCRCS_REGNUM, read_memory_integer(new_sp - 16, 4));
  508.     }
  509.   write_register (PC_REGNUM, FRAME_SAVED_PC(frame));
  510.   if (frame->proc_desc) {
  511.     for (regnum = 32; --regnum >= 0; )
  512.       if (PROC_REG_MASK(proc_desc) & (1 << regnum))
  513.     write_register (regnum,
  514.           read_memory_integer (frame->saved_regs->regs[regnum], 4));
  515.     for (regnum = 32; --regnum >= 0; )
  516.       if (PROC_FREG_MASK(proc_desc) & (1 << regnum))
  517.     write_register (regnum + FP0_REGNUM,
  518.           read_memory_integer (frame->saved_regs->regs[regnum + FP0_REGNUM], 4));
  519.   }
  520.   write_register (SP_REGNUM, new_sp);
  521.   flush_cached_frames ();
  522.   set_current_frame (create_new_frame (new_sp, read_pc ()));
  523. }
  524.  
  525. static void
  526. mips_print_register(regnum, all)
  527.      int regnum, all;
  528. {
  529.       unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE];
  530.       REGISTER_TYPE val;
  531.  
  532.       /* Get the data in raw format.  */
  533.       if (read_relative_register_raw_bytes (regnum, raw_buffer))
  534.     {
  535.       printf_filtered ("%s: [Invalid]", reg_names[regnum]);
  536.       return;
  537.     }
  538.       
  539.       /* If an even floating pointer register, also print as double. */
  540.       if (regnum >= FP0_REGNUM && regnum < FP0_REGNUM+32
  541.       && !((regnum-FP0_REGNUM) & 1)) {
  542.       read_relative_register_raw_bytes (regnum+1, raw_buffer+4);
  543.       printf_filtered ("(d%d: ", regnum-FP0_REGNUM);
  544.       val_print (builtin_type_double, raw_buffer, 0,
  545.              stdout, 0, 1, 0, Val_pretty_default);
  546.       printf_filtered ("); ");
  547.       }
  548.       fputs_filtered (reg_names[regnum], stdout);
  549. #ifndef NUMERIC_REG_NAMES
  550.       if (regnum < 32)
  551.       printf_filtered ("(r%d): ", regnum);
  552.       else
  553. #endif
  554.       printf_filtered (": ");
  555.  
  556.       /* If virtual format is floating, print it that way.  */
  557.       if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (regnum)) == TYPE_CODE_FLT
  558.       && ! INVALID_FLOAT (raw_buffer, REGISTER_VIRTUAL_SIZE(regnum))) {
  559.       val_print (REGISTER_VIRTUAL_TYPE (regnum), raw_buffer, 0,
  560.              stdout, 0, 1, 0, Val_pretty_default);
  561.       }
  562.       /* Else print as integer in hex.  */
  563.       else
  564.     {
  565.       long val;
  566.  
  567.       bcopy (raw_buffer, &val, sizeof (long));
  568.       SWAP_TARGET_AND_HOST ((char *)&val, sizeof (long));
  569.       if (val == 0)
  570.         printf_filtered ("0");
  571.       else if (all)
  572.         printf_filtered (local_hex_format(), val);
  573.       else
  574.         printf_filtered ("%s=%d", local_hex_string(val), val);
  575.     }
  576. }
  577.  
  578. /* Replacement for generic do_registers_info.  */
  579. void
  580. mips_do_registers_info (regnum, fpregs)
  581.      int regnum;
  582.      int fpregs;
  583. {
  584.   if (regnum != -1) {
  585.       mips_print_register (regnum, 0);
  586.       printf_filtered ("\n");
  587.   }
  588.   else {
  589.       for (regnum = 0; regnum < NUM_REGS; ) {
  590.       if ((!fpregs) && regnum >= FP0_REGNUM && regnum <= FCRIR_REGNUM) {
  591.         regnum++;
  592.         continue;
  593.       }
  594.       mips_print_register (regnum, 1);
  595.       regnum++;
  596.       if ((regnum & 3) == 0 || regnum == NUM_REGS)
  597.           printf_filtered (";\n");
  598.       else
  599.           printf_filtered ("; ");
  600.       }
  601.   }
  602. }
  603. /* Return number of args passed to a frame. described by FIP.
  604.    Can return -1, meaning no way to tell.  */
  605.  
  606. int
  607. mips_frame_num_args(fip)
  608.     FRAME fip;
  609. {
  610. #if 0
  611.     struct chain_info_t *p;
  612.  
  613.     p = mips_find_cached_frame(FRAME_FP(fip));
  614.     if (p->valid)
  615.         return p->the_info.numargs;
  616. #endif
  617.     return -1;
  618. }
  619.  
  620.  
  621. /* Bad floats: Returns 0 if P points to a valid IEEE floating point number,
  622.    1 if P points to a denormalized number or a NaN. LEN says whether this is
  623.    a single-precision or double-precision float */
  624. #define SINGLE_EXP_BITS  8
  625. #define DOUBLE_EXP_BITS 11
  626. int
  627. isa_NAN(p, len)
  628.      int *p, len;
  629. {
  630.   int exponent;
  631.   if (len == 4)
  632.     {
  633.       exponent = *p;
  634.       exponent = exponent << 1 >> (32 - SINGLE_EXP_BITS - 1);
  635.       return ((exponent == -1) || (! exponent && *p));
  636.     }
  637.   else if (len == 8)
  638.     {
  639.       exponent = *(p+1);
  640.       exponent = exponent << 1 >> (32 - DOUBLE_EXP_BITS - 1);
  641.       return ((exponent == -1) || (! exponent && *p * *(p+1)));
  642.     }
  643.   else return 1;
  644. }
  645.  
  646. /*
  647.  * Implemented for Irix 4.x by Garrett A. Wollman
  648.  */
  649. #ifdef USE_PROC_FS        /* Target-dependent /proc support */
  650.  
  651. #include <sys/time.h>
  652. #include <sys/procfs.h>
  653.  
  654. typedef unsigned int greg_t;    /* why isn't this defined? */
  655.  
  656. /*
  657.  * See the comment in m68k-tdep.c regarding the utility of these functions.
  658.  */
  659.  
  660. void 
  661. supply_gregset (gregsetp)
  662.      gregset_t *gregsetp;
  663. {
  664.   register int regno;
  665.   register greg_t *regp = (greg_t *)(gregsetp->gp_regs);
  666.  
  667.   /* FIXME: somewhere, there should be a #define for the meaning
  668.      of this magic number 32; we should use that. */
  669.   for(regno = 0; regno < 32; regno++)
  670.     supply_register (regno, (char *)(regp + regno));
  671.  
  672.   supply_register (PC_REGNUM, (char *)&(gregsetp->gp_pc));
  673.   supply_register (HI_REGNUM, (char *)&(gregsetp->gp_mdhi));
  674.   supply_register (LO_REGNUM, (char *)&(gregsetp->gp_mdlo));
  675.   supply_register (PS_REGNUM, (char *)&(gregsetp->gp_cause));
  676. }
  677.  
  678. void
  679. fill_gregset (gregsetp, regno)
  680.      gregset_t *gregsetp;
  681.      int regno;
  682. {
  683.   int regi;
  684.   register greg_t *regp = (greg_t *)(gregsetp->gp_regs);
  685.   extern char registers[];
  686.  
  687.   /* same FIXME as above wrt 32*/
  688.   for (regi = 0; regi < 32; regi++)
  689.     if ((regno == -1) || (regno == regi))
  690.       *(regp + regno) = *(greg_t *) ®isters[REGISTER_BYTE (regi)];
  691.  
  692.   if ((regno == -1) || (regno == PC_REGNUM))
  693.     gregsetp->gp_pc = *(greg_t *) ®isters[REGISTER_BYTE (PC_REGNUM)];
  694.  
  695.   if ((regno == -1) || (regno == PS_REGNUM))
  696.     gregsetp->gp_cause = *(greg_t *) ®isters[REGISTER_BYTE (PS_REGNUM)];
  697.  
  698.   if ((regno == -1) || (regno == HI_REGNUM))
  699.     gregsetp->gp_mdhi = *(greg_t *) ®isters[REGISTER_BYTE (HI_REGNUM)];
  700.  
  701.   if ((regno == -1) || (regno == LO_REGNUM))
  702.     gregsetp->gp_mdlo = *(greg_t *) ®isters[REGISTER_BYTE (LO_REGNUM)];
  703. }
  704.  
  705. /*
  706.  * Now we do the same thing for floating-point registers.
  707.  * We don't bother to condition on FP0_REGNUM since any
  708.  * reasonable MIPS configuration has an R3010 in it.
  709.  *
  710.  * Again, see the comments in m68k-tdep.c.
  711.  */
  712.  
  713. void
  714. supply_fpregset (fpregsetp)
  715.      fpregset_t *fpregsetp;
  716. {
  717.   register int regno;
  718.  
  719.   for (regno = 0; regno < 32; regno++)
  720.     supply_register (FP0_REGNUM + regno,
  721.              (char *)&fpregsetp->fp_r.fp_regs[regno]);
  722.  
  723.   supply_register (FCRCS_REGNUM, (char *)&fpregsetp->fp_csr);
  724.  
  725.   /* FIXME: how can we supply FCRIR_REGNUM?  SGI doesn't tell us. */
  726. }
  727.  
  728. void
  729. fill_fpregset (fpregsetp, regno)
  730.      fpregset_t *fpregsetp;
  731.      int regno;
  732. {
  733.   int regi;
  734.   char *from, *to;
  735.   extern char registers[];
  736.  
  737.   for (regi = FP0_REGNUM; regi < FP0_REGNUM + 32; regi++)
  738.     {
  739.       if ((regno == -1) || (regno == regi))
  740.     {
  741.       from = (char *) ®isters[REGISTER_BYTE (regi)];
  742.       to = (char *) &(fpregsetp->fp_r.fp_regs[regi]);
  743.       bcopy(from, to, REGISTER_RAW_SIZE (regno));
  744.     }
  745.     }
  746.  
  747.   if ((regno == -1) || (regno == FCRCS_REGNUM))
  748.     fpregsetp->fp_csr = *(unsigned *) ®isters[REGISTER_BYTE(FCRCS_REGNUM)];
  749. }
  750.  
  751. #endif /* USE_PROC_FS */
  752.  
  753. /* To skip prologues, I use this predicate. Returns either PC
  754.    itself if the code at PC does not look like a function prologue,
  755.    PC+4 if it does (our caller does not need anything more fancy). */
  756.  
  757. CORE_ADDR
  758. mips_skip_prologue(pc)
  759.      CORE_ADDR pc;
  760. {
  761.     struct symbol *f;
  762.     struct block *b;
  763.     unsigned long inst;
  764.     int offset;
  765.  
  766.     /* For -g modules and most functions anyways the
  767.        first instruction adjusts the stack.
  768.        But we allow some number of stores before the stack adjustment.
  769.        (These are emitted by varags functions compiled by gcc-2.0. */
  770.     for (offset = 0; offset < 100; offset += 4) {
  771.     inst = read_memory_integer(pc + offset, 4);
  772.     if ((inst & 0xffff0000) == 0x27bd0000) /* addiu $sp,$sp,offset */
  773.         return pc + offset + 4;
  774.     if ((inst & 0xFFE00000) != 0xAFA00000) /* sw reg,n($sp) */
  775.         break;
  776.     }
  777.  
  778.     /* Well, it looks like a frameless. Let's make sure.
  779.        Note that we are not called on the current PC,
  780.        but on the function`s start PC, and I have definitely
  781.        seen optimized code that adjusts the SP quite later */
  782.     b = block_for_pc(pc);
  783.     if (!b) return pc;
  784.  
  785.     f = lookup_symbol(".gdbinfo.", b, LABEL_NAMESPACE, 0, NULL);
  786.     if (!f) return pc;
  787.     /* Ideally, I would like to use the adjusted info
  788.        from mips_frame_info(), but for all practical
  789.        purposes it will not matter (and it would require
  790.        a different definition of SKIP_PROLOGUE())
  791.  
  792.        Actually, it would not hurt to skip the storing
  793.        of arguments on the stack as well. */
  794.     if (((mips_extra_func_info_t)SYMBOL_VALUE(f))->pdr.frameoffset)
  795.     return pc + 4;
  796.  
  797.     return pc;
  798. }
  799.  
  800. /* Figure out where the longjmp will land.
  801.    We expect the first arg to be a pointer to the jmp_buf structure from which
  802.    we extract the pc (JB_PC) that we will land at.  The pc is copied into PC.
  803.    This routine returns true on success. */
  804.  
  805. int
  806. get_longjmp_target(pc)
  807.      CORE_ADDR *pc;
  808. {
  809.   CORE_ADDR jb_addr;
  810.  
  811.   jb_addr = read_register(A0_REGNUM);
  812.  
  813.   if (target_read_memory(jb_addr + JB_PC * JB_ELEMENT_SIZE, pc,
  814.              sizeof(CORE_ADDR)))
  815.     return 0;
  816.  
  817.   SWAP_TARGET_AND_HOST(pc, sizeof(CORE_ADDR));
  818.  
  819.   return 1;
  820. }
  821.